﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
using BIPorter.Common.BLL;
using BIPorter.Common.Model;

namespace BIPorter.Common.CommonFilter
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class BIAuthorizeFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext != null)
            {
                bool legal = false;
                AccountBll accountBll = new BLL.AccountBll();
                MenuBll menuBll = new MenuBll();
                AccountModel accountModel = null;
                /*
                 * 1获取当前登录用户权限
                 * 2获取当前Action
                 * 3响应处理
                 */
                var filterContextInfo = new FilterContextInfo(filterContext);

                if (filterContext.HttpContext.Request.IsAuthenticated)
                {
                    accountModel = AccountBll.GetCurrentUser();
                    if (accountModel != null)
                    {
                        legal = menuBll.IsLegl(filterContextInfo, accountModel.Role);
                    }
                }

                if (!legal)
                {
                    if (filterContextInfo.actionName == "Default")
                    {
                        filterContext.Result = new RedirectResult(FormsAuthentication.LoginUrl);
                    }
                    else
                    {
                        var tipInfo = "抱歉, 权限不够！";
                        if (accountModel == null)
                        {
                            tipInfo = "会话已过期,请刷新页面！";
                        }
                        filterContext.Result = new ContentResult { Content = tipInfo };
                    }
                }
            }
        }
    }

    public class FilterContextInfo
    {
        public FilterContextInfo(ActionExecutingContext filterContext)
        {
            #region 获取链接中的字符
            // 获取域名
            domainName = filterContext.HttpContext.Request.Url.Authority;

            //获取模块名称
            // module = filterContext.HttpContext.Request.Url.Segments[1].Replace('/', ' ').Trim();

            //获取 controllerName 名称
            controllerName = filterContext.RouteData.Values["controller"].ToString();

            //获取ACTION 名称
            actionName = filterContext.RouteData.Values["action"].ToString();

            #endregion
        }
        /// <summary>
        /// 获取域名
        /// </summary>
        public string domainName { get; set; }
        /// <summary>
        /// 获取模块名称
        /// </summary>
        public string module { get; set; }
        /// <summary>
        /// 获取 controllerName 名称
        /// </summary>
        public string controllerName { get; set; }
        /// <summary>
        /// 获取ACTION 名称
        /// </summary>
        public string actionName { get; set; }

    }
}
